package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sxit.tdf.server.entity.FilePath;
import com.sxit.tdf.server.entity.bo.SimpleFilePathInfoBo;
import com.sxit.tdf.server.entity.vo.DeleteFileFoldervVo;
import com.sxit.tdf.server.entity.vo.InsertFileFolderVo;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IFilePathService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 橙子高
 * @since 2022-02-23
 */
@RestController
@RequestMapping("/file-path")
@Tag(name = "FilePathController", description = "文件夹管理类")
public class FilePathController {

  @Autowired private IFilePathService filePathService;

  @Operation(summary = "创建文件夹")
  @PostMapping("createFileFolder")
  @SaCheckPermission("FILEFOLAD:INSERT")
  public R createFileFolder(@Valid @RequestBody InsertFileFolderVo insertFileFolderVo) {

    Boolean isCreate = filePathService.createFileFolder(insertFileFolderVo);

    return R.ok();
  }

  @Operation(summary = "删除文件夹")
  @PostMapping("deleteFileFolder")
  @SaCheckPermission("FILEFOLAD:DELETE")
  public R deleteFileFolder(@Valid @RequestBody DeleteFileFoldervVo deleteFileFoldervVo) {

    FilePath fileByPathCode = filePathService.findFileByPathCode(deleteFileFoldervVo.getId());
    if (fileByPathCode != null) {
      Boolean isCreate = filePathService.deleteFileFolder(fileByPathCode.getId());
      return R.ok("删除成功");
    }

    return R.ok("删除失败");
  }

  @Operation(summary = "获取文件夹信息")
  @GetMapping("/getFolderList")
  //  @SaCheckPermission("FILEFOLAD:SELECT")
  public R getFolderList() {

    HashMap<String, List<SimpleFilePathInfoBo>> folderList = filePathService.getFolderList();

    return R.ok().put("data", folderList);
  }

  @Operation(summary = "根据筒的Id获取文件夹信息")
  @GetMapping("/getFolderListByBucketId")
  public R getFolderListByBucketId(@RequestParam("id") Integer id) {

    HashMap<String, List<SimpleFilePathInfoBo>> folderList =
        filePathService.getFolderListByBucketId(id);

    return R.ok().put("data", folderList);
  }
}
